var mongoose =require('mongoose');
const Comment=require('../models/comment_models');
//新建评论
exports.create = function(req,res,next){
    const com = new Comment(req.body);
    com.save().then((data) =>{
        res.json(data);
    });
}

exports.update = function(req,res,next){
    const id = req.params.id;
    // res.send(id);
    Comment.findByIdAndUpdate(id,{$set: req.body},{new:false})
    .then(data=>{
        res.json(data); // 这里返回的数据是修改之前的数据，不是修改之后的数据
    })
}

// 移除相应的id的数据
exports.remove = function(req,res,next){
    const id = req.params.id;
    Comment.findByIdAndDelete(id,(err,data)=>{
        res.json(data);
    })
}

// 这个暂时还没搞清楚
exports.list = function(req,res,next){
    var page  = req.body.page ? req.body.page : 1;
    var limit = req.body.limit ? req.body.limit :5;

    var queryCondition = {};  //查询条件
    if(req.body.name && req.body.name.trim().length>0){
        name           = req.body.name;
        queryCondition = {
            "name": new RegExp(name,'i')
        }
    }

    Comment.paginate(queryCondition, { page: +page, limit: +limit }, function(err, result) {
        result.rows = result.docs;
        delete result.docs;
        res.json(result);
      });

}

// 查询相应id信息
exports.getData = function(req,res,next){
    var id = req.params.id;
    Comment.findById(id).then(data=>{
        res.json(data);
    })

}

exports.removes=function(req,res,next){
    var ids=req.body.ids.split(',')
    console.log(ids);
        if(ids.length>0){
            Comment.remove({_id: {$in: ids}}).then(data=>{
                res.json({'message':'delete ok',status:200})
            })
        }
}
